//
//  PickerContentViewController.m
//  photoLibrayIos8
//
//  Created by jing zhao on 4/22/16.
//  Copyright © 2016 jing zhao. All rights reserved.
//

#import "PickerContentViewController.h"
#import "PickerHeader.h"
#import "UploadViewController.h"

@interface PickerContentViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong ,nonatomic)NSDictionary *dicData;
@property (strong ,nonatomic)NSMutableArray *arraySelect;
@property (weak, nonatomic) IBOutlet UIButton *buttonSelect;
@property (weak, nonatomic) IBOutlet UILabel *labelSelect;

@end

@implementation PickerContentViewController


-(void)viewDidLoad{
    BOOL boolOriginSelect = [[self.dicData objectForKey:KEYSelect]boolValue];
    self.buttonSelect.selected = boolOriginSelect;
    self.labelSelect.text = [NSString stringWithFormat:@"%lu/9  selected",(unsigned long)[self.arraySelect count]];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
    
    [[PHImageManager defaultManager]requestImageForAsset:[self.dicData objectForKey:KEYAsset]
                                              targetSize:[UIScreen mainScreen].bounds.size
                                             contentMode:PHImageContentModeAspectFill
                                                 options:options
                                           resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                                               [self.imageView setImage:result];;
                                           }];
}

-(void)configureData:(NSDictionary*)dicData andSelectArray:(NSMutableArray*)array{
    self.dicData = dicData;
    self.arraySelect = [NSMutableArray arrayWithArray:array];
    
    
}

- (IBAction)back:(id)sender {
    BOOL boolOriginSelect = [[self.dicData objectForKey:KEYSelect]boolValue];
    if (boolOriginSelect != self.buttonSelect.selected) {
        [self.delegatePickContent delegateSelectPicker:self.dicData];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)buttonSelect:(id)sender {
    if (!self.buttonSelect.selected) {
        if ([self.arraySelect count]+1>9) {
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"消息" message:@"你已选择超过9个" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
            [alert show];
        }else{
            self.buttonSelect.selected = YES;
            [self.arraySelect addObject:self.dicData];
            self.labelSelect.text = [NSString stringWithFormat:@"%lu/9  selected",(unsigned long)[self.arraySelect count]];
        }
        
    }else{
        self.buttonSelect.selected = NO;
        [self.arraySelect removeObject:self.dicData];
        self.labelSelect.text = [NSString stringWithFormat:@"%lu/9  selected",(unsigned long)[self.arraySelect count]];
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.destinationViewController isKindOfClass:[UploadViewController class]]){
        if([self.arraySelect count]> 0){
            UploadViewController *UploadView = (UploadViewController*)segue.destinationViewController;
            [UploadView setDataArray: self.arraySelect];
        }else{
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"消息" message:@"你没有选中任何图片" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
            [alertView show];
        }
    }
}

@end
